package org.uim.cricket.storage.queries;

import java.util.Map;
import java.util.Set;

import org.uim.cricket.entities.FullGround;
import org.uim.cricket.entities.match.CricketMatch;

public class GroundQueryGenerator extends QueryGenerator {

	private FullGround ground = null;
	private String displayName = null;
	private String continent = null;
	
	public GroundQueryGenerator(CricketMatch match, FullGround ground, String displayName, String continent) {
		super(match);
		this.ground = ground;
		this.displayName = displayName;
		this.continent = continent;
	}
	
	@Override
	public String generateQuery() throws Exception {
		StringBuilder query = new StringBuilder();
		query.append("CALL sp_Insert_Ground(");
		query.append(this.ground.getId()).append(",");
		this.appendQuery(query, encodeSQL(this.ground.getFullName()), 256, false);
		this.appendQuery(query, encodeSQL(this.displayName), 48, true);
		this.appendQuery(query, encodeSQL(this.ground.getTown()), 32, false);
		this.appendQuery(query, encodeSQL(this.ground.getCountry()), 128, true);
		this.appendQuery(query, encodeSQL(continent), 128, false, false);
		query.append(");\n");
		
		if (this.ground.getAttribs() != null && this.ground.getAttribs().size() > 0) {
			query.append(this.insertAttributes(this.ground.getAttribs())).append("\n");
		}
		if (this.ground.getHomeTeams() != null && this.ground.getHomeTeams().size() > 0) {
			query.append(this.insertTeams(this.ground.getHomeTeams())).append("\n");
		}
		
		return query.toString();
	}
	
	private String insertAttributes(Map<String, String> attributes) throws Exception {
		StringBuilder allq = new StringBuilder();
		for (String key : attributes.keySet()) {
			if (key.equalsIgnoreCase("Address")) continue;
			
			String value = attributes.get(key);
			
			StringBuilder query = new StringBuilder("CALL sp_Insert_GroundDetail(");
			query.append(this.ground.getId()).append(",");
			this.appendQuery(query, encodeSQL(key), 64, false);
			this.appendQuery(query, encodeSQL(value), 256, false, false);
			query.append(");\n");
			
			allq.append(query.toString());
			
		}
		return allq.toString();
	}

	private String insertTeams(Set<String> homeTeams) throws Exception {
		StringBuilder allq = new StringBuilder();
		for (String team : homeTeams) {
			StringBuilder query = new StringBuilder("CALL sp_Insert_GroundDetail(");
			query.append(this.ground.getId()).append(",");
			this.appendQuery(query, "Home teams", 20, false);
			this.appendQuery(query, encodeSQL(team), 128, false, false);
			query.append(");");
			
		}
		return allq.toString();
	}
	
	
	public void setGround(FullGround ground) {
		this.ground = ground;
	}
	
	public FullGround getGround() {
		return ground;
	}
	
	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}
	
	public void setContinent(String continent) {
		this.continent = continent;
	}
	
	public String getDisplayName() {
		return displayName;
	}
	
	public String getContinent() {
		return continent;
	}
}
